#!/usr/bin/env python

import os, sys
from webform_class import WebForm

def add_field ( liwe, id_field = '', name = '', field = '', args = '', pos = '' ):
	"""
	:INPUT:
		- id_field:		SAFE_NUMBER	
		- name:			SAFE_DB_STRING	
		- field:		SAFE_DB_STRING	
		- args:			SAFE_STRING	
		- pos:			SAFE_NUMBER	
	:xPERMS:

	"""
	wf = WebForm ( liwe )
	
	return { "form_fields" : wf.add_field ( id_field = id_field, name = name, field = field, args = args, pos = pos ) }


def get_field ( liwe, id_field ):
	"""
	:INPUT:
		- id_field: 	SAFE_NUMBER
	:PERMS:
                - create:               With this permission you can create/modify
	"""
	wf = WebForm ( liwe )
	return {  "get_field": wf.get_field ( id_field = id_field ) }
		

def del_fields ( liwe, ids = '' ):
	"""
	:INPUT:
		- ids:			SAFE_STRING
	:xPERMS:

	"""
	wf = WebForm ( liwe )
	
	wf.del_fields ( ids )  

def check_form_name ( liwe, name = '' ):
	"""
	:INPUT:
		- name:			SAFE_DB_STRING
	:xPERMS:

	"""
	wf = WebForm ( liwe )
	if not name:
		return { "name" : '' }

	cn = wf.check_form_name ( name )
	if not cn: 
		fname = ''
	else:
		fname = cn [ 'name' ]
	return { "name" : fname  }

def list ( liwe, fname = '', _X_LINES = 10, _X_START_POINT = 0, _X_PAGE = 0 ):
        """     
        :INPUT:         
                - fname: 		SAFE_DB_STRING        
                - _X_LINES:             SAFE_NUMBER
                - _X_START_POINT:       SAFE_NUMBER
                - _X_PAGE:              SAFE_NUMBER
                                
        :PERMS:                 
                - create:               With this permission you can create/modify
        """
                        
        if _X_START_POINT:
                start_point = _X_START_POINT
        else:   
                start_point = _X_PAGE * _X_LINES
                        
        limit = _X_LINES        
                                
        wf = WebForm ( liwe )
        res = wf.list ( fname, start_point, limit )

	if not res: res = {}
                        
        count = 0
        for v in res.get ( 'rows', [] ):
                v [ "_pos" ] = start_point + count
                res [ "row%d" % count ] = v
        
                v [ '_img' ] = ''

                count += 1 
                
        res [ 'rows' ] = res.get ( 'tot_rows', 0 )
                
        res [ 'from_row' ] = start_point
        res [ 'to_row' ] = start_point + count
        res [ 'lines' ] = _X_LINES
                
        return res 


def list_mails ( liwe, _X_LINES = 10, _X_START_POINT = 0, _X_PAGE = 0 ):
        """     
        :INPUT:         
                - _X_LINES:             SAFE_NUMBER
                - _X_START_POINT:       SAFE_NUMBER
                - _X_PAGE:              SAFE_NUMBER
                                
        :PERMS:                 
                - create:               With this permission you can create/modify
        """
                        
        if _X_START_POINT:
                start_point = _X_START_POINT
        else:   
                start_point = _X_PAGE * _X_LINES
                        
        limit = _X_LINES        
                                
        wf = WebForm ( liwe )
        res = wf.list_mails ( start_point, limit )

	if not res: res = {}
                        
        count = 0
        for v in res.get ( 'rows', [] ):
                v [ "_pos" ] = start_point + count
                res [ "row%d" % count ] = v
        
                count += 1 
                
        res [ 'rows' ] = res.get ( 'tot_rows', 0 )
                
        res [ 'from_row' ] = start_point
        res [ 'to_row' ] = start_point + count
        res [ 'lines' ] = _X_LINES
                
        return res 

def edit_mail ( liwe, id_destination = '', descr = '', email = '' ):
	"""
	:INPUT:
		- id_destination		SAFE_DB_NUMBER
		- descr				SAFE_DB_STRING
		- email				SAFE_DB_STRING
	"""
	wf = WebForm ( liwe )
	wf.edit_mail ( id_destination, descr, email )


def del_mail ( liwe, ids = '' ):
	"""
	:INPUT:
		- ids:			SAFE_STRING
	:xPERMS:

	"""
	wf = WebForm ( liwe )
	
	wf.del_mail ( ids )  

def set_destination ( liwe, mail_ids = '', fname = '' ):
        """
        :INPUT:
                - fname: form name      SAFE_DB_STRING
                - mail_ids:             SAFE_DB_STRING
        """
        wf = WebForm ( liwe )
        res = wf.set_destination ( mail_ids , fname )
        if not res:
                res = []
        return { "destinations" : res }

def clean_rel_table ( liwe, fname = '', optimize = '' ):
        """
        :INPUT:
                - fname: form name      SAFE_DB_STRING
                - optimize: any value   SAFE_STRING
	"""	
	wf = WebForm ( liwe )
	wf.clean_rel_table ( fname, optimize )

def move_field ( liwe, start_id = '' , move = '' ):
	"""
	:INPUT:
		- start_id 	SAFE_DB_NUMBER	
		- move	 	SAFE_STRING	
	"""
	wf = WebForm ( liwe )
	wf.move_field ( start_id, move )


def main_set ( liwe, name = '', tpl_name = '', sender = 0, subject = '' ):
	"""
	:INPUT:
		- name		SAFE_STRING
		- tpl_name	SAFE_STRING
		- sender	SAFE_NUMBER
		- subject	SAFE_STRING
	:PERMS:
                - create
	"""
	wf = WebForm ( liwe )
	return { 'main_set' : wf.main_set ( name = name, tpl_name = tpl_name, sender = sender, subject = subject ) }

def main_update ( liwe, name = '', tpl_name = '', tpl = -1, sender = 0 ):
	"""
	:INPUT:
		- name		SAFE_STRING
		- tpl_name	SAFE_STRING
		- tpl		SAFE_NUMBER
		- sender	SAFE_NUMBER
	:PERMS:
                - create
	"""
	wf = WebForm ( liwe )
	return { 'main_update' : wf.main_update ( name = name, tpl = tpl, tpl_name = tpl_name, sender = sender ) }
	


def main_get ( liwe, fname ):
	"""
	:INPUT:
		- name		SAFE_STRING
	:PERMS:
                - create
	"""
	wf = WebForm ( liwe )
	return { "main_get" : wf.main_get ( name  = fname ) }

def main_list ( liwe ):
	"""
	:PERMS:
                - create
	"""
	wf = WebForm ( liwe )
	return { 'main_list' : wf.main_list () }
	



